package org.springblade.modules.system.service.impl.idcardauth;

import org.springblade.modules.system.service.IdCardAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description: 身份证实名认证组件
 * @author: luoyt
 * @create: 2024-08-13 23:13
 **/
@Component
public class IdCardAuthComponent {

    private final Map<String, IdCardAuthService> authServices;

    @Autowired
    public IdCardAuthComponent(List<IdCardAuthService> authServiceList) {
        this.authServices = authServiceList.stream()
                .collect(Collectors.toMap(authService -> authService.getClass().getSimpleName(), authService -> authService));
    }

    /**
     * 身份证验证
     * @param platform
     * @param name
     * @param idNumber
     * @return boolean
     */
    public boolean authenticate(String platform, String name, String idNumber) {
        IdCardAuthService authService = authServices.get(platform);
        if (authService != null) {
            return authService.authenticate(name, idNumber);
        } else {
            throw new IllegalArgumentException("IdCard Unsupported authentication platform: " + platform);
        }
    }
}